package com.study.framework;


import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.study.tests.ContactData;
import com.study.utils.SortedListOf;

public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicaionManager mananger) {
		super(mananger);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		
	cachedContacts = new SortedListOf<ContactData>();
		
		List<WebElement> firstNameColumn = driver.findElements(By.xpath("//input[@name='selected[]']/../../td[2]"));
		for (WebElement column : firstNameColumn) {
			String title = column.getText();
			String lastname = title;
			cachedContacts.add(new ContactData().withLastName(lastname));
		}
	}

	public ContactHelper createContact(ContactData contact) {
		contactsPage();
		fillContactForm(contact);
		submitContactCreation();
		manager.navigateTo().contactsList();
		rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);
		fillContactForm(contact);
		submitContactModification();
		manager.navigateTo().contactsList();
		rebuildCache();
		return this;
		
	}

	public ContactHelper deleteContact(int index) {
		initContactModification(index);
		contactDeletion();
		manager.navigateTo().contactsList();
		rebuildCache();
		return this;
	}

//-------------------------------------------------------------------------	
	
	public ContactHelper submitContactCreation() {
		click(By.xpath("//input[@name='submit']"));
		cachedContacts = null;
		return this;	
	}

	public ContactHelper fillContactForm(ContactData contact) {
		type(By.name("firstname"), contact.getFirstName());
		// Thread.sleep(1000);
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress1());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		type(By.name("byear"), contact.getByYear());
		selectByText(By.name("bday"), contact.getByDay());
		selectByText(By.name("bmonth"), contact.getByMonth());
		// selectByText(By.name("new_group"), contact.groupname);
		return this;

	}

	public ContactHelper initContactModification(int index) {
		click(By.xpath("//tr[@name='entry']["+ (index + 1) +"]/td[7]//img"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;
	}
	
	public ContactHelper contactsPage() {
		click(By.xpath("//a[contains(text(),'add new')]"));
		return this;
	}

	private ContactHelper contactDeletion() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
		return this;
	}
}